package com.mallcloud.mall.admin.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.admin.api.dto.SysLogDTO;
import com.mallcloud.mall.admin.api.entity.SysLog;
import com.mallcloud.mall.admin.api.vo.PreLogVO;
import com.mallcloud.mall.admin.service.SysLogService;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.excel.annotation.ResponseExcel;
import com.mallcloud.mall.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "日志管理模块")
@RestController
@AllArgsConstructor
@RequestMapping("/log")
public class SysLogController {
	private final SysLogService sysLogService;

	@ApiOperation("简单分页查询")
	@GetMapping("/page")
	public R<IPage<SysLog>> getLogPage(Page page, SysLogDTO sysLog) {
		return R.ok(sysLogService.getLogByPage(page, sysLog));
	}

	@ApiOperation("删除日志")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_log_del')")
	public R<Boolean> removeById(@PathVariable Long id) {
		return R.ok(sysLogService.removeById(id));
	}

	@ApiOperation("批量删除日志")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('sys_log_del')")
	public R<Boolean> removeByIds(@RequestBody Long[] ids) {
		return R.ok(sysLogService.removeBatchByIds(CollUtil.toList(ids)));
	}

	@ApiOperation("插入日志")
	@Inner
	@PostMapping("/save")
	public R<Boolean> save(@Valid @RequestBody SysLogDTO sysLog) {
		return R.ok(sysLogService.saveLog(sysLog));
	}

	@ApiOperation("批量插入日志")
	@PostMapping("/logs")
	public R<Boolean> saveBatchLogs(@RequestBody List<PreLogVO> preLogVoList) {
		return R.ok(sysLogService.saveBatchLogs(preLogVoList));
	}

	@ApiOperation("导出excel 表格")
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('sys_log_export')")
	public List<SysLog> export(SysLog sysLog) {
		return sysLogService.list(Wrappers.query(sysLog));
	}

}
